/*
 * AbstractSQLMarcadorDAO.java
 *
 * Created on 7 de noviembre de 2007, 20:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package webportlet.model.marcador.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import webportlet.model.marcador.dto.MarcadorDTO;
import webportlet.model.util.DuplicateInstanceException;
import webportlet.model.util.GeneralOperations;
import webportlet.model.util.InstanceNotFoundException;
import webportlet.model.util.InternalErrorException;

/**
 *
 * @author Alvarete
 */
public abstract class AbstractSQLMarcadorDAO implements SQLMarcadorDAO{
    
    public abstract MarcadorDTO create(Connection connection, MarcadorDTO marcadorDTO) 
        throws DuplicateInstanceException, InternalErrorException;
    
    public boolean exists(Connection connection, Long id)
        throws InternalErrorException {
        
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            /* Create "preparedStatement". */
            String queryString = "SELECT idMarcador FROM Marcador" +
                " WHERE idMarcador = ?";
            preparedStatement = connection.prepareStatement(queryString);
            
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setLong(i++, id);
            
            /* Execute query. */
            resultSet = preparedStatement.executeQuery();

            return resultSet.next();
            
        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }

    public MarcadorDTO find(Connection connection, Long id) 
        throws InstanceNotFoundException, InternalErrorException {
        
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            /* Create "preparedStatement". */
            String queryString = "SELECT nombre,url FROM Marcador" +
                    " WHERE idMarcador = ?";
            
            preparedStatement = connection.prepareStatement(queryString);
            
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setLong(i++, id);
            
            /* Execute query. */
            resultSet = preparedStatement.executeQuery();
            
            if (!resultSet.next()) {
                throw new InstanceNotFoundException(id, 
                        MarcadorDTO.class.getName());
            }

            /* Get results. */
            i = 1;
            String nombre = resultSet.getString(i++);
            String url = resultSet.getString(i++);
            
            /* Return the value object. */
            return new MarcadorDTO(id,nombre,url);
            
        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }    
    }

    public void update(Connection connection, MarcadorDTO marcadorDTO) 
        throws InstanceNotFoundException, InternalErrorException {
        
        PreparedStatement preparedStatement = null;

        try {

            /* Create "preparedStatement". */
            String queryString = "UPDATE Marcador SET nombre = ?, url = ?" +
                " WHERE idMarcador = ?";
            preparedStatement = connection.prepareStatement(queryString);
            
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setString(i++, marcadorDTO.getNombre());
            preparedStatement.setString(i++, marcadorDTO.getUrl());
            preparedStatement.setLong(i++, marcadorDTO.getId());
            
            /* Execute query. */
            int updatedRows = preparedStatement.executeUpdate();

            if (updatedRows == 0) {
                throw new InstanceNotFoundException(
                    marcadorDTO.getId(), 
                    MarcadorDTO.class.getName());
            }

            if (updatedRows > 1) {
                throw new SQLException("Duplicate row for idMarcador = '" + 
                    marcadorDTO.getId() + "' in table 'Marcador'");
            }        
            
        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        } 
    }

    public void remove(Connection connection, Long id) 
        throws InstanceNotFoundException, InternalErrorException {
        
        PreparedStatement preparedStatement = null;
        
        try {

            /* Create "preparedStatement". */
            String queryString = "DELETE FROM Marcador WHERE" +
                " idMarcador = ?";
            preparedStatement = connection.prepareStatement(queryString);
            
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setLong(i++, id);
            
            /* Execute query. */
            int removedRows = preparedStatement.executeUpdate();

            if (removedRows == 0) {
                throw new InstanceNotFoundException(id,
                    MarcadorDTO.class.getName());
            }

        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }    
    
    }
    public ArrayList <MarcadorDTO> recuperarLista(Connection connection, 
            ArrayList <Long> listaIdentificadores)
            throws InstanceNotFoundException, InternalErrorException{
        
        ArrayList <MarcadorDTO> listaMarcadores = new ArrayList();
        for(int i=0; i<listaIdentificadores.size(); i++){
            
            if(!listaMarcadores.add(this.find(connection,
                    listaIdentificadores.get(i)))){
                throw new InstanceNotFoundException(listaIdentificadores.get(i),
                    MarcadorDTO.class.getName());
            }
        }
        return listaMarcadores;
    }
}
